﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xeml.Document;
using System.Diagnostics;
using XemlUtilities;
using System.IO;
using XemlUtilities.DataSet;
using Xeml.Sampling.Contracts;
using Xeml.SQLDataProviders;

namespace XemlTest
{
    [TestClass]
    public class UtIsaExporter
    {
        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod(), Ignore()]
        public void UpdateExperimentsInDb()
        {
            //foreach (Guid xemlId in General.GetXemlIds(Properties.Settings.Default.GmdConnectionString))
            foreach (Guid xemlId in General.GetXemlIds())
            {
                //XemlDocument xemldoc = new XemlDocument(XemlDataSet.LoadXeml(xemlId, Properties.Settings.Default.GmdConnectionString));
                XemlDocument xemldoc = General.GetDocument(xemlId);
                IDataProvider dataProvider = new GmdDataProvider();
                XemlDataSet.Save(xemldoc, dataProvider, (string s, Guid g) => { return true; });
            }
        }

        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod(), Ignore()]
        public void XemlHashCode()
        {
            IDataProvider dataProvider = new GmdDataProvider();
            Assert.AreEqual(7, GetXemlHashCodes(new XemlDataSet(General.GetDocument(Guid.Parse("BA487064-8B46-4408-812C-B4E5083AD7C2")), dataProvider)).Count, "expected 7 replicagroups");
            Assert.AreEqual(16, GetXemlHashCodes(new XemlDataSet(General.GetDocument(Guid.Parse("57545514-4007-4234-9BA0-F37FB2794081")), dataProvider)).Count, "expected 16 replicagroups");

            List<string> res = GetXemlHashCodes(new XemlDataSet(General.GetDocument(Guid.Parse("57545514-4007-4234-9BA0-F37FB2794081")), dataProvider)).Select(x => Convert.ToBase64String(BitConverter.GetBytes(x))).ToList();
        }

        private List<int> GetXemlHashCodes(XemlDataSet xds)
        {
            return xds.Sample.Select(x => x.GetXemlHashCode()).Distinct().ToList();
        }

        [TestCategory("Nightly"), TestCategory("Weekly"), TestCategory("ShoppingCart"), TestMethod()]
        public void TestIsaExport()
        {
            isaExport(Guid.Parse("b38df13d-55b6-47d2-9e6a-5ef878ecaeab"));
            //isaExport(Guid.Parse("10b38aaf-b977-4950-85b8-f4775f66658d"));
            //isaExport(Guid.Parse("5f135f69-aff6-4d0a-8f7a-2f54be22d3e9"));

            //isaExportFromFile(@"c:\Users\Billiau\Downloads\xpo.xeml");
            
            //isaExport(Guid.Parse("061dbdeb-f3dd-4afa-acfd-54e83479c3dd"));

            //foreach (Guid g in General.GetXemlIds())
            //{
            //    isaExport(g);
            //}
        }

        private void isaExportFromFile(String path)
        {
            Guid xemlId = Guid.Parse("061dbdeb-f3dd-4afa-acfd-54e83479c3dd");
            XemlDocument xdoc;
            using (StreamReader sr = new StreamReader(path))
            {
                xdoc = new XemlDocument(sr.ReadToEnd());
            }

            GmdDataProvider gmd = new GmdDataProvider(Properties.Settings.Default.GmdConnectionString);
            using (XemlDataSet xds = XemlDataSet.GetInstance(xdoc, gmd))
            using (System.IO.Stream zipFile = System.IO.File.Open(string.Format(@"C:\Users\Billiau\Downloads\{0}.zip", xds.Document.Single().PutativeFileName), System.IO.FileMode.Create))
            using (XemlUtilities.ISA.IsaExporter exporter = new XemlUtilities.ISA.IsaExporter(xemlId, Properties.Settings.Default.GmdConnectionString))
            {
                Debug.WriteLine("starting export {0}", xemlId);
                exporter.Write(xds, zipFile);
                Debug.WriteLine("done export {0}", xemlId);
                Debug.Assert(zipFile.Position > 0);
                zipFile.Close();
            }
        }

        private void isaExport(Guid xemlId)
        {
            DataProviderConfiguration c = new DataProviderConfiguration();
            c.ConnectionString = Properties.Settings.Default.GmdConnectionString;
            IDataProvider dataProvider = new GmdDataProvider(c);
            using (XemlDataSet xds = XemlDataSet.GetInstance(xemlId, dataProvider))
            using (System.IO.Stream zipFile = System.IO.File.Open(string.Format(@"{0}\Downloads\{1}.zip", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), xds.Document.Single().PutativeFileName), System.IO.FileMode.Create))
            using (XemlUtilities.ISA.IsaExporter exporter = new XemlUtilities.ISA.IsaExporter(xemlId, Properties.Settings.Default.GmdConnectionString))
            {
                Debug.WriteLine("starting export {0}", xemlId);
                exporter.Write(xds, zipFile);
                Debug.WriteLine("done export {0}", xemlId);
                Debug.Assert(zipFile.Position > 0);
                zipFile.Close();
            }
        }
    }
}
